import 'package:flutter/foundation.dart';

enum LogLevel {
  debug,
  info,
  warning,
  error,
}

class LogService {
  static final LogService _instance = LogService._internal();
  factory LogService() => _instance;
  LogService._internal();

  // 在生产环境中设置为info或更高级别
  LogLevel _currentLevel = kDebugMode ? LogLevel.debug : LogLevel.info;

  // 设置日志级别
  void setLogLevel(LogLevel level) {
    _currentLevel = level;
  }

  // 调试日志
  void debug(String message) {
    if (_currentLevel.index <= LogLevel.debug.index) {
      debugPrint('DEBUG: $message');
    }
  }

  // 信息日志
  void info(String message) {
    if (_currentLevel.index <= LogLevel.info.index) {
      debugPrint('INFO: $message');
    }
  }

  // 警告日志
  void warning(String message) {
    if (_currentLevel.index <= LogLevel.warning.index) {
      debugPrint('WARNING: $message');
    }
  }

  // 错误日志
  void error(String message, [dynamic error, StackTrace? stackTrace]) {
    if (_currentLevel.index <= LogLevel.error.index) {
      debugPrint('ERROR: $message');
      if (error != null) {
        debugPrint('ERROR DETAILS: $error');
      }
      if (stackTrace != null) {
        debugPrint('STACK TRACE: $stackTrace');
      }
    }
  }
}
